//
//  NGbbsNavigationBarView.swift
//  NingguoForum
//
//  Created by zhengsw on 2025/7/29.
//  Copyright © 2025 zhengsw. All rights reserved.
//

import UIKit
import RxSwift
import SnapKit

// MARK: - Protocol
public protocol NGbbsNavigationBarViewDelegate: AnyObject {
    func goUserInfoAction()
    func searchAction()
}

// MARK: - Main Class
class NGbbsNavigationBarView: UIView {
    
    // MARK: - Properties
    weak var delegate: NGbbsNavigationBarViewDelegate?
    private let disposeBag = DisposeBag()
    
    // MARK: - UI Components
    private lazy var userButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "user_icon"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.accessibilityLabel = "用户信息"
        return button
    }()
    
    private lazy var searchBtn: UIButton = {
        let button = UIButton(type: .custom)

        button.backgroundColor = RGB_COLOR(r: 248, g: 248, b: 248)
        button.setTitle("搜索", for: .normal)

        // 设置文本靠左对齐
       button.contentHorizontalAlignment = .left
        // 使用新的Configuration API
       var configuration = UIButton.Configuration.plain()
       configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
        configuration.titleAlignment = .leading
        // 设置字体
        configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
            var outgoing = incoming
            outgoing.font = UIFont.systemFont(ofSize: 15)
            outgoing.foregroundColor = UIColor.red
            return outgoing
        }
        
        button.configuration = configuration
        
        button.layer.cornerRadius = 8

        return button
    }()
    
    private lazy var editButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "edit"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.accessibilityLabel = "编辑"
        return button
    }()
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupBindings()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
        setupBindings()
    }
    
    // MARK: - Setup Methods
    private func setupUI() {
        backgroundColor = .white
        
        addSubview(userButton)
        addSubview(searchBtn)
        addSubview(editButton)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
        userButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 44, height: 44))
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(KDevice_StatusBar_Height)
        }
        
        editButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 44, height: 44))
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview().offset(KDevice_StatusBar_Height)
        }
        
        searchBtn.snp.makeConstraints { make in
            make.centerY.equalTo(userButton)
            make.left.equalTo(userButton.snp.right).offset(20)
            make.right.equalTo(editButton.snp.left).offset(-20)
            make.height.equalTo(34)
        }
    }
    
    private func setupBindings() {
        // 用户按钮点击
        userButton.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.handleUserButtonTap()
            })
            .disposed(by: disposeBag)
        
        // 编辑按钮点击
        editButton.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.handleEditButtonTap()
            })
            .disposed(by: disposeBag)
        
        searchBtn.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.handleSearchButtonTap()
            })
            .disposed(by: disposeBag)
    }
    
    // MARK: - Action Handlers
    private func handleUserButtonTap() {
        delegate?.goUserInfoAction()
    }
    private func handleSearchButtonTap() {
        delegate?.searchAction()
    }
    private func handleEditButtonTap() {
        print("跳登录")
        // 这里可以添加具体的登录逻辑
    }

}

